package cn.kgc.vue.service.impl;

import cn.kgc.vue.common.ResponseResult;
import cn.kgc.vue.common.ResponseStatus;
import cn.kgc.vue.dto.LogPageDTO;
import cn.kgc.vue.entity.LogInfo;
import cn.kgc.vue.mapper.LogInfoMapper;
import cn.kgc.vue.service.LogInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;


@Service
public class LogInfoServiceImpl extends ServiceImpl<LogInfoMapper, LogInfo> implements LogInfoService {

    @Resource
    private LogInfoMapper logInfoMapper;

    @Override
    public ResponseResult getPage(LogPageDTO logPageDTO) {

        ResponseResult responseResult = new ResponseResult();

        Page<LogInfo> logInfoPage = new Page<>(logPageDTO.getCurrentPage(), logPageDTO.getLimit());
        LambdaQueryWrapper<LogInfo> lambda = new QueryWrapper<LogInfo>().lambda();
        lambda.eq(StringUtils.isNotBlank(logPageDTO.getUsername()), LogInfo::getUsername, logPageDTO.getUsername())
                .eq(StringUtils.isNotBlank(logPageDTO.getIp()), LogInfo::getIp, logPageDTO.getIp())
                .eq(StringUtils.isNotBlank(logPageDTO.getModel()), LogInfo::getModel, logPageDTO.getModel())
                .orderByDesc(LogInfo::getCreateTime);

        logInfoMapper.selectPage(logInfoPage, lambda);

        HashMap<String, Object> map = new HashMap<>();
        map.put("total", logInfoPage.getTotal());
        map.put("currentPageData", logInfoPage.getRecords());
        responseResult.success(ResponseStatus.SUCCESS, map);

        return responseResult;
    }

    @Override
    public void deleteLogInfo() {
        logInfoMapper.deleteLog();
    }
}




